<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 12.10.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodeFolder
    ****
    * This classs represents a folder
    ****
    * @package clientNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 12.10.2005
    * @copyright essentialmind.com 2005
    ***/
    class clientNodeFolder extends clientNode{
        
        private $pageType;
        
        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){
            
            //first figure out the ID of the datatype responsible for inserats
            $this->setPageType();

            //first I need to get the IDs of all child nodes of this folder
            /*
                Please note, that a whole array of all child nodes is fetched as well,
                as it will be passed to smarty in order to present on the interface
                that there exist subfolders in the selected folder
            */
            $myTree     = new tree($this->db,'structure LEFT JOIN objectmap ON structure.id = objectmap.nodeid',$this->log,'id, level, path_cache AS path, title, published_pages');
            $nodes      = $myTree->getChildren($this->node);
            $children   = array();  // list of node IDs that lie below the current node
            $subcats    = array();  // list of direct subcategories i.e. no grandchildren
            
            //mark the level of the subcategories
            if(isset($nodes[0])){
                $curLevel   = $nodes[0]['level'];
            }
            
            $children[] = $this->node;  //add current node to the set limiter
            
            //loop through the node array and create associate markers to pass to smarty
            foreach($nodes as $node){
                $children[] = $node['id'];
                if($node['level'] == $curLevel){
                    $subcats[] = $node;   
                }
            }
            
            
            //get news items for this folder
            /*
                the call to get the full path to the node may be
                a bit costly. This procedure may need refactoring
            */
            $items   = $this->db->getAll("SELECT 
                                                    pm.created          AS intime,
                                                    pm.title            AS title,
                                                    ni.content          AS content,
                                                    ni.id               AS id,
                                                    get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                    'files/pages/'||pm.pagetype||'__'||pm.pageid||'/image/' AS imagePath
                                                FROM pagemap AS pm 
                                                LEFT JOIN node_page AS ni ON pm.pageid = ni.id
                                                WHERE pm.nodeid IN (".implode(',',$children).") 
                                                AND pm.pagetype = " . $this->pageType . "
                                                AND pm.wf_state = 1
                                                ORDER BY pm.published DESC");
            
            //get folder properties if any
            $data = $this->db->getRow("SELECT * FROM node_folder WHERE id = ".$this->object);
            $data['title'] = $this->db->getOne("SELECT title FROM objectmap WHERE version = 1 AND nodeid = ".$this->node);
            
            //get actual folder data
            //assign all this stuff to smarty
            $this->smarty->assign(array(
                                    'childFolders'  => $subcats,
                                    'items'         => $items,
                                    'data'          => $data));
            
        }
        
        /**
         * set the action template
         * 
         * @return void
         **/
        public function setTemplate(){
            $this->setActionTemplate('folder.html');
        }
        
        
        /**
         * Sets the ID of the pagetype corresponsing to an inserat
         *
         */
        private function setPageType(){
            $this->pageType = (int)$this->db->getOne("SELECT id FROM datatype WHERE codeset = 'page'");
            if($this->pageType == 0){
                throw new Exception("No Folder Item Datatype Registered with the system!");   
            }
        }
    }
?>
